package com.liwei.blog.pojo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.MappedSuperclass;
import javax.persistence.Table;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;



/**
* @author liwei
* @version 创建时间：2018年12月1日 下午2:31:13
* 类说明
*/
@Entity
//@Table(name = "user") //缺一个state用户状态，后面补上
public class User implements UserDetails, Serializable {
	
	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
	@NotEmpty(message="用户名不能为空")
	@Size(min=1,max=15)
	@Column(name="username",nullable=true,unique=true,columnDefinition="varchar(15) COMMENT '用户名'",length=15)
    private String username;
	
	@NotEmpty(message="密码不能为空")
	@Size(min=1,max=100)
	@Column(name="password",nullable=true,columnDefinition="varchar(100)  COMMENT '密码'",length=100)
    private String password;
	
	@NotEmpty(message="邮箱不能为空")
	@Size(min=1,max=20)
	@Email(message="邮箱格式不对")
	@Column(name="email",nullable=true,columnDefinition="varchar(20)  COMMENT  '邮箱'",length=20)
	private String email;
	
	@Column(name="avatar",nullable=true,columnDefinition="varchar(100)  default '/images/1.jpg' COMMENT '头像图片地址'")
	private String avatar;
	
	@ManyToMany(cascade = CascadeType.DETACH, fetch = FetchType.EAGER)
	@JoinTable(name = "user_authority", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), 
		inverseJoinColumns = @JoinColumn(name = "authority_id", referencedColumnName = "id"))
	private List<Authority> authorities;
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public void setEncodePassword(String password) {
		PasswordEncoder  encoder = new BCryptPasswordEncoder();
		String encodePasswd = encoder.encode(password);
		this.password = encodePasswd;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}


	
	public String getAvatar() {
		return avatar;
	}
	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + ", avater="
				+ avatar + ", authorities=" + authorities + "]";
	}
	public void setAuthorities(List<Authority> authorities) {
		this.authorities = authorities;
	}
	
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		// TODO Auto-generated method stub
	//  需将 List<Authority> 转成 List<SimpleGrantedAuthority>，否则前端拿不到角色列表名称
			List<SimpleGrantedAuthority> simpleAuthorities = new ArrayList<>();
			for(GrantedAuthority permission : this.authorities){
				simpleAuthorities.add(new SimpleGrantedAuthority(permission.getAuthority()));
			}
			return simpleAuthorities;
	}
	//账户是否过期,过期无法验证
	@Override
	public boolean isAccountNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}
	//指定用户是否被锁定或者解锁,锁定的用户无法进行身份验证
	@Override
	public boolean isAccountNonLocked() {
		// TODO Auto-generated method stub
		return true;
	}
	//指示是否已过期的用户的凭据(密码),过期的凭据防止认证
	@Override
	public boolean isCredentialsNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}
	//是否被禁用,禁用的用户不能身份验证
	@Override
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return true;
	}
	
}
